///this file holds all user interface functions

nothal.update_selection = function() {
	var selection = nothal.selection;
	if( 'undefined' != typeof selection ){ this.selection = selection; }
	this.selected.innerHTML = "";
	if( 'undefined' == typeof this.selection ){
		this.selected.innerHTML = "Click on a crew member to select";
	} else { //the only selectable items are crew members
		var c = this.selection;
		this.selected.innerHTML = build_crewman_table(c);
	}
}

function build_crewman_table(c)
{
	var output = "<table border='0' cellpadding='0' width='100%' height='99%'><tr><td>";
	output += c.roleid+"</td><td>"+c.name+"</td></tr><tr><td>Health:</td><td/>"+c.health+"</td></tr><tr><td>Location:</td><td/>"+Math.floor(c.x)+","+Math.floor(c.y)+"</td></tr>";
	return output;
}

function start() {
	//console.log("start");
	if( nothal.stopped ) {
		//console.log("starting!");
		nothal.stopped = false;
		nothal.counter = nothal.gametime;
		nothal.countdisplay.innerHTML = ""+nothal.counter;
		nothal.rooms = genmaze( nothal.width, nothal.height );
		humane.log("Map Generated");
		nothal.crew = make_crew( nothal );
		humane.log( ""+nothal.crew.length+" Crew Recruited" );
		nothal.paused = true;
		resume();
		playMusic( nothal, document.getElementById('backgroundMusic') );
	}
}

function resume() {
	if( nothal.paused ) {
		nothal.paused = false;
		nothal.mainloop = setInterval( nothal.gameloop, nothal.steptime );
		nothal.countloop = setInterval( nothal.countdown, 1000 ); //countdown is in seconds
		nothal.running = true;
	}
}

function pause() {
	if(nothal.running) {
		nothal.running = false;
		window.clearInterval( nothal.mainloop );
		window.clearInterval( nothal.countloop );
		nothal.paused = true;
	}
}

function stop( nothal, message ) {
	//console.log("stop");
	if(nothal.running) {
		//console.log(message);
		if( 'undefined' == typeof message ) { message = "start game"; }
		pause();
		nothal.paused = false;
		//console.log("paused");
		//humane.log(message);
		nothal_redraw( nothal );
		nothal.countdisplay.innerHTML = message;
		document.getElementById("master").innerHTML="Start";
		nothal.stopped = true;
	}
}

function toggle_run_state() {
	if( nothal.running ) { pause(); return "Resume"; } //pause a running game
	else if( nothal.paused ) { resume(); return "Pause"; } //resume a paused game
	else { start(); return "Pause"; } //start a new game
}

function toggle_mute_state( nothal )
{
	if(nothal.soundENABLE)
	{
		//console.log("muting")
		nothal.soundENABLE = false;
		pauseMusic( nothal );
		for( var i=0; i<nothal.playing.length; i++ ) { nothal.playing[i].pause(); }
		nothal.playing = [];
		return "Unmute";
	}
	else
	{
		//console.log("unmuting")
		nothal.soundENABLE = true;
		playMusic( nothal );
		return "Mute";
	}
}

function Smack_selected()
{
	if( 'undefined' != typeof nothal.selection && nothal.running)
	{
		nothal.selection.health-=10; 
		//humane.log("You Smack the Crap out of "+nothal.selection.name+".");
		if(nothal.selection.health<=0)
		{
			humane.log(nothal.selection.name+" Dies.");
			nothal.crew.splice(nothal.crew.indexOf(nothal.selection),1);
			nothal.selection = undefined;
			play("death");
			if( 0 == nothal.crew.length ) { //victory!
				playMusic( nothal, document.getElementById("winMusic") );
				stop( nothal, "You Win!" );
			}
		}
	}
//	else if(nothal.running) { humane.error("You need a valid Target"); }
//	else if( nothal.paused ) { humane.error( "The Game is paused. You cheater."); }
//	else { humane.error( "Too late, you already lost!" ); }
}